home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CBody.as next >
Encoding:
Text File  |  2007-07-13  |  9.5 KB  |  343 lines

  1. class CBody extends MovieClip implements ISimulated
  2. {
  3.    var m_velocity;
  4.    var m_acceleration;
  5.    var m_drag;
  6.    var m_forceAccumulator;
  7.    var m_impulseAccumulator;
  8.    var m_lastLocation;
  9.    var m_collisionVolume;
  10.    var m_arrCollisionBuckets;
  11.    var m_attachedBodies;
  12.    var m_effectiveVelocity;
  13.    var m_isAttached;
  14.    var _location;
  15.    var m_mass = 1;
  16.    var m_restitution = 1;
  17.    var m_dragLinear = 0.8;
  18.    var m_dragAngular = 0.8;
  19.    var m_maxSpeed = -1;
  20.    var m_doRotateAttached = false;
  21.    var m_doUpdateRotation = true;
  22.    var m_doesCollide = true;
  23.    var m_isSimulating = true;
  24.    var m_nUpdates = 0;
  25.    var m_nImpulses = 0;
  26.    var m_angularVelocity = 0;
  27.    var m_angularAcceleration = 0;
  28.    var m_torqueAccumulator = 0;
  29.    var m_lastUpdateTime = 0;
  30.    var m_maxOldLocations = 3;
  31.    var m_didMove = false;
  32.    function CBody()
  33.    {
  34.       super();
  35.       FreshDebug.Assert(FreshFramework._simulation != undefined,"FreshFramework._simulation");
  36.       this.m_velocity = new Vector2D(0,0);
  37.       this.m_acceleration = new Vector2D(0,0);
  38.       this.m_drag = new Vector2D(0,0);
  39.       this.m_forceAccumulator = new Vector2D(0,0);
  40.       this.m_impulseAccumulator = new Vector2D(0,0);
  41.       this.m_lastLocation = new Vector2D(this._x,this._y);
  42.       this.m_lastUpdateTime = FreshFramework._simulation.GetNamedClock(this.GetClockName());
  43.       if(!this.m_collisionVolume)
  44.       {
  45.          this.CreateDefaultCollisionVolume();
  46.       }
  47.       FreshFramework._simulation.RegisterBody(this);
  48.       this.m_arrCollisionBuckets = new Array();
  49.       this.m_attachedBodies = new Array();
  50.    }
  51.    function Destroy()
  52.    {
  53.       FreshFramework._simulation.UnregisterBody(this);
  54.       this.VacatePriorCollisionBuckets();
  55.       this.swapDepths(0);
  56.       this.removeMovieClip();
  57.    }
  58.    function CreateDefaultCollisionVolume()
  59.    {
  60.       this.m_collisionVolume = new CCircle(this,Math.max(this._width,this._height) * 0.5);
  61.    }
  62.    function get _updatePriority()
  63.    {
  64.       return 0;
  65.    }
  66.    function get _location()
  67.    {
  68.       return new Vector2D(this._x,this._y);
  69.    }
  70.    function set _location(loc)
  71.    {
  72.       this._x = loc._x;
  73.       this._y = loc._y;
  74.    }
  75.    function GetLocation()
  76.    {
  77.       return new Vector2D(this._x,this._y);
  78.    }
  79.    function GetWorldSpaceLocation()
  80.    {
  81.       var _loc4_ = this._location;
  82.       var _loc3_ = this._parent;
  83.       while(_loc3_ != _root)
  84.       {
  85.          _loc4_.Add(new Vector2D(_loc3_._x,_loc3_._y));
  86.          _loc3_ = _loc3_._parent;
  87.       }
  88.       return _loc4_;
  89.    }
  90.    function GetWorldSpaceRotation()
  91.    {
  92.       var _loc4_ = this._rotation;
  93.       var _loc3_ = this._parent;
  94.       while(_loc3_ != _root)
  95.       {
  96.          _loc4_ += _loc3_._rotation;
  97.          _loc3_ = _loc3_._parent;
  98.       }
  99.       return _loc4_;
  100.    }
  101.    function get _lastLocation()
  102.    {
  103.       return this.m_lastLocation;
  104.    }
  105.    function get _velocity()
  106.    {
  107.       return this.m_velocity.GetCopy();
  108.    }
  109.    function get _effectiveVelocity()
  110.    {
  111.       return this.m_effectiveVelocity;
  112.    }
  113.    function get _acceleration()
  114.    {
  115.       return this.m_acceleration;
  116.    }
  117.    function get _forceAccumulator()
  118.    {
  119.       return this.m_forceAccumulator.GetCopy();
  120.    }
  121.    function get _speed()
  122.    {
  123.       return this.m_velocity.MagnitudeSafe();
  124.    }
  125.    function get _doesCollide()
  126.    {
  127.       return this.m_collisionVolume && this.m_doesCollide;
  128.    }
  129.    function get _didMove()
  130.    {
  131.       return this.m_didMove;
  132.    }
  133.    function get _topLeftCorner()
  134.    {
  135.       if(this.m_collisionVolume)
  136.       {
  137.          return this.m_collisionVolume._topLeftCorner;
  138.       }
  139.       return this._location;
  140.    }
  141.    function get _bottomRightCorner()
  142.    {
  143.       if(this.m_collisionVolume)
  144.       {
  145.          return this.m_collisionVolume._bottomRightCorner;
  146.       }
  147.       return this._location;
  148.    }
  149.    function get _boundingRadius()
  150.    {
  151.       if(this.m_collisionVolume)
  152.       {
  153.          return this.m_collisionVolume._boundingRadius;
  154.       }
  155.       return Math.max(this._width * 0.5,this._height * 0.5);
  156.    }
  157.    function get _maxSpeed()
  158.    {
  159.       return this.m_maxSpeed;
  160.    }
  161.    function get _angularVelocity()
  162.    {
  163.       return this.m_angularVelocity;
  164.    }
  165.    function get _nUpdates()
  166.    {
  167.       return this.m_nUpdates;
  168.    }
  169.    function get _arrCollisionBuckets()
  170.    {
  171.       return this.m_arrCollisionBuckets;
  172.    }
  173.    function AttachBody(body)
  174.    {
  175.       if(body)
  176.       {
  177.          this.m_attachedBodies.push(body);
  178.          body.m_isAttached = true;
  179.       }
  180.    }
  181.    function NotifyAddedToBucket(bucket)
  182.    {
  183.       this.m_arrCollisionBuckets.push(bucket);
  184.    }
  185.    function VacatePriorCollisionBuckets()
  186.    {
  187.       var _loc3_ = this.m_arrCollisionBuckets.length;
  188.       var _loc2_ = 0;
  189.       while(_loc2_ < _loc3_)
  190.       {
  191.          this.m_arrCollisionBuckets[_loc2_].RemoveBody(this);
  192.          _loc2_ = _loc2_ + 1;
  193.       }
  194.       this.m_arrCollisionBuckets.splice(0);
  195.    }
  196.    function NotifyCollision(collisionInfo)
  197.    {
  198.    }
  199.    function ApplyForce(vecForce)
  200.    {
  201.       if(this.m_mass > 0)
  202.       {
  203.          this.m_forceAccumulator.Add(vecForce.GetDivideScalar(this.m_mass));
  204.       }
  205.    }
  206.    function ApplyThrust(vecForce)
  207.    {
  208.       var _loc2_ = new Vector2D(vecForce._x,vecForce._y);
  209.       _loc2_.Rotate(MathUtil.DegreesToRadians(this._rotation));
  210.       this.ApplyForce(_loc2_);
  211.    }
  212.    function ApplyImpulse(vecImpulse)
  213.    {
  214.       if(this.m_mass > 0)
  215.       {
  216.          this.m_nImpulses = this.m_nImpulses + 1;
  217.          this.m_impulseAccumulator.Add(vecImpulse);
  218.       }
  219.    }
  220.    function ApplyGravity(gravity)
  221.    {
  222.       if(this.m_mass > 0)
  223.       {
  224.          this.m_forceAccumulator.Add(gravity);
  225.       }
  226.    }
  227.    function ApplyTorque(torque)
  228.    {
  229.       if(this.m_mass > 0)
  230.       {
  231.          this.m_torqueAccumulator += torque;
  232.       }
  233.    }
  234.    function GetClockName()
  235.    {
  236.       return "";
  237.    }
  238.    function PreUpdate()
  239.    {
  240.       this.m_didMove = false;
  241.    }
  242.    function Update()
  243.    {
  244.       this.m_nUpdates = this.m_nUpdates + 1;
  245.       var _loc4_ = this.GetClockName();
  246.       var _loc3_ = FreshFramework._simulation.GetNamedDeltaTime(_loc4_);
  247.       if(!this.m_isAttached && this.m_isSimulating && this.m_mass > 0 && !FreshFramework._simulation.GetNamedIsPaused(_loc4_) && _loc3_ > 0)
  248.       {
  249.          this.m_lastUpdateTime = FreshFramework._simulation.GetNamedClock(_loc4_);
  250.          if(this.m_doUpdateRotation)
  251.          {
  252.             this.m_angularAcceleration = this.m_torqueAccumulator;
  253.             this.m_angularVelocity += this.m_angularAcceleration * _loc3_;
  254.             this.m_angularVelocity -= this.m_angularVelocity * Math.min(this.m_dragAngular * _loc3_,1);
  255.             this._rotation += this.m_angularVelocity * _loc3_;
  256.          }
  257.          if(this.m_nImpulses > 0)
  258.          {
  259.             this.m_impulseAccumulator.DivideScalar(this.m_nImpulses);
  260.             this.m_velocity.Set(this.m_impulseAccumulator);
  261.          }
  262.          this.m_acceleration.Set(this.m_forceAccumulator);
  263.          this.m_velocity.Add(this.m_acceleration.GetMultiplyScalar(_loc3_));
  264.          this.m_velocity.Subtract(this.m_velocity.GetMultiplyScalar(Math.min(this.m_dragLinear * _loc3_,1)));
  265.          if(this.m_drag.MagnitudeSquared() > 0)
  266.          {
  267.             var _loc5_ = MathUtil.DegreesToRadians(this._rotation);
  268.             this.m_velocity.Rotate(- _loc5_);
  269.             this.m_velocity.Subtract(this.m_velocity.GetMultiply(this.m_drag.GetMultiplyScalar(_loc3_)));
  270.             this.m_velocity.Rotate(_loc5_);
  271.          }
  272.          if(this._maxSpeed >= 0 && this._speed > this._maxSpeed)
  273.          {
  274.             this.m_velocity.Normalize();
  275.             this.m_velocity.MultiplyScalar(this._maxSpeed);
  276.          }
  277.          if(this.m_velocity.MagnitudeSquared() < 0.01)
  278.          {
  279.             this.m_velocity.SetToZero();
  280.          }
  281.          this._x += this.m_velocity._x * _loc3_;
  282.          this._y += this.m_velocity._y * _loc3_;
  283.          var _loc2_ = 0;
  284.          while(_loc2_ < this.m_attachedBodies.length)
  285.          {
  286.             this.m_attachedBodies[_loc2_]._location = this._location;
  287.             if(this.m_doRotateAttached)
  288.             {
  289.                this.m_attachedBodies[_loc2_]._rotation = this._rotation;
  290.             }
  291.             _loc2_ = _loc2_ + 1;
  292.          }
  293.       }
  294.    }
  295.    function UpdateConstraints()
  296.    {
  297.    }
  298.    function PostUpdate()
  299.    {
  300.       this.m_didMove = this.m_nUpdates == 1 || this._location.GetDistanceSquared(this.m_lastLocation) > 0.0001;
  301.       this.m_effectiveVelocity = this._location.GetSubtract(this.m_lastLocation);
  302.       this.m_lastLocation = this._location;
  303.       this.m_forceAccumulator.SetToZero();
  304.       this.m_impulseAccumulator.SetToZero();
  305.       this.m_nImpulses = 0;
  306.       this.m_torqueAccumulator = 0;
  307.    }
  308.    function Rollback(amount)
  309.    {
  310.       this._location = this.m_lastLocation;
  311.    }
  312.    function GetDelta(body)
  313.    {
  314.       return new Vector2D(this._x - body._x,this._y - body._y);
  315.    }
  316.    function GetDistance(body)
  317.    {
  318.       return this.GetDelta(body).Magnitude();
  319.    }
  320.    function GetDistanceSquared(body)
  321.    {
  322.       return this.GetDelta(body).MagnitudeSquared();
  323.    }
  324.    function DoesSuppressCentralizedCollisionResolution()
  325.    {
  326.       return false;
  327.    }
  328.    function DoesCollideWith(body)
  329.    {
  330.       if(!this._doesCollide || !body._doesCollide)
  331.       {
  332.          return null;
  333.       }
  334.       var _loc2_ = Intersection.DoIntersect(this.m_collisionVolume,body.m_collisionVolume);
  335.       if(_loc2_)
  336.       {
  337.          _loc2_.m_body1 = this;
  338.          _loc2_.m_body2 = body;
  339.       }
  340.       return _loc2_;
  341.    }
  342. }
  343.